<template>
	<view class="content" v-if="show==1">
		<view class="chong">
			<view class="chong2" v-for="(m,index) in money">
				<text>㉿{{m.quan}}</text>
				<view class="chong3" @click="pay" :data-index="index">￥{{m.money}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	var app=getApp();
	var iapChannel = null;
	export default {
		data() {
			return {
				uid:0,
				show:0,
				type:0,
				is_pay:false,
				money:[]
			}
		},
		onLoad(op) {
			const uid = uni.getStorageSync('uid');
			if(app.isBlank(uid)==true){
				app.error_msg("请先登录");
			}
			uni.showLoading({
			    title: '数据加载中'
			});
			var that=this;
			plus.payment.getChannels((channels) => {
				console.log("获取到channel" + JSON.stringify(channels))
				for (var i in channels) {
					var channel = channels[i];
					if (channel.id === 'appleiap') {
						iapChannel = channel;
						that.requestOrder();
					}
				}
				
				if(!iapChannel){
					that.show=1;
					uni.hideLoading();
				}
			}, (error) => {
				that.show=1;
				uni.hideLoading();
			});
			if(app.isBlank(op.type)==false){
				this.type=1;
			}
			this.uid=uid;
			this.getdata();
		},
		methods: {
			getdata(){
				var that=this;
				
				uni.request({
				    url: app.globalData.url+"user/chong",
					method:"POST",
					dataType:"json",
					header: {
					    'content-type': 'application/x-www-form-urlencoded'
					},
				    data: {uid:that.uid},
				    success: (res) => {
						
						that.money=res.data.data.money
				    }
				});
			},
			requestOrder() {
				var that=this;
				var productIds=['020','021','022','023','024','025','026','027','028','029'];
				iapChannel.requestOrder(productIds, (orderList) => { //必须调用此方法才能进行 iap 支付
					console.log('requestOrder success666: ' + JSON.stringify(orderList));
					that.is_pay=true;
					that.show=1;
					uni.hideLoading();
				}, (e) => {
					that.show=1;
					uni.hideLoading();
					console.log('requestOrder failed: ' + JSON.stringify(e));
				});
			},
			pay(e){
				if(this.is_pay==false){
					uni.showToast({
					    title: '不支持的支付方式',
						icon:'error',
					    duration: 1500
					});
					return false;
				}
				var index=e.currentTarget.dataset.index;
				var that=this;
				uni.showLoading({
				    title: '请求中',
					mask:true
				});
				
				uni.request({
				    url: app.globalData.url+"user/dochong",
					method:"POST",
					dataType:"json",
					header: {
					    'content-type': 'application/x-www-form-urlencoded'
					},
				    data: {uid:that.uid,id:that.money[index].id},
				    success: (res) => {
						uni.hideLoading();
						if(res.data.code!=1){
							uni.showToast({
							    title: res.data.msg,
								icon:'none',
							    duration: 1500
							});
							return false;
						}
						uni.showLoading({
						    title: '支付中..',
							mask:true
						});
						plus.payment.request(iapChannel, {
							productid: that.money[index].code,  
							username: "", // 用户标识  
							optimize: true  // 设置 optimize: true 解决丢单问题  
						}, function(result) {
							// 支付成功，result 为 IAP商品交易信息对象 IAPTransaction 需将返回的支付凭证传给后端进行二次认证  
							uni.hideLoading();
							uni.showLoading({
								title: '支付结果校验中..'
							});
							console.log(result);
							uni.request({
								url:app.globalData.url+"user/chongres",
								method:"POST",
								dataType:"json",
								header: {
									'content-type': 'application/x-www-form-urlencoded'
								},
								data:{uid:that.uid,code:result.transactionReceipt,id:res.data.data.id,transactionid:result.transactionIdentifier},
								success(res3) {
									uni.hideLoading();
									if(res3.data.code!=1){
										uni.showToast({
											title: res3.data.msg,
											icon:'none',
											duration: 1500
										});
										return false;
									}
									uni.showToast({
										title: "支付成功",
										icon:'success',
										duration: 1000
									});
									setTimeout(function(){
										var pages = getCurrentPages(); //当前页面栈
										if (pages.length > 1){
											if(that.type==1){
												var beforePage = pages[pages.length - 2];//上个页面
												// #ifdef H5
												beforePage.getdata();
												// #endif
												// #ifndef H5
												beforePage.$vm.getdata();
												// #endif
											}
											uni.navigateBack({
												delta: 1
											});
										}else{
											uni.switchTab({
											    url: '/pages/user/index'
											});
										}
									},1000)
								},
								fail(e) {
							
								}
							})
						}, function(e) {
							// 支付失败的时候需要调用一下 restoreComplateRequest 方法
							uni.hideLoading();
							that.restoreComplateRequest()
						});
				    }
				});
			},
			password: function(event) {
			    this.pwd.password = event.target.value
			},
			newpwd: function(event) {
			    this.pwd.newpwd = event.target.value
			},
			qrpwd: function(event) {
			    this.pwd.qrpwd = event.target.value
			}
		}
	}
</script>

<style>
	.chong3{width:80px;height:35px;line-height: 35px;text-align: center;color:#fff;background-color: #05c160;border-radius: 4px;float: right;margin-top: 7.5px;}
	.chong2 text{color:red;font-size: 1.1rem;}
	.chong2{width:94%;margin: 0 auto;height:50px;line-height: 50px;border-bottom: 1px #eee solid;}
	.chong{width:100%;}
</style>
